<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('business.quote.normal.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="100px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('business.quote.normal.publish_time')" prop="publish_time">
                    <el-date-picker v-model="dataForm.publish_time" type="date" :placeholder="i18n('common.please_input') + i18n('business.quote.normal.publish_time')" clearable/>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('business.information.harbor_code')" prop="harbor_code">
                    <el-select
                      v-model="dataForm.harbor_code"
                      :placeholder="i18n('common.please_select') + i18n('business.quote.normal.harbor_code')" :remote-method="loadHarborList" :loading="loading" reserve-keyword filterable remote clearable>
                      <el-option v-for="(v, k) in harborList" :label="v.english_name" :key="k" :value="v.code"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('business.quote.normal.machine_id')" prop="machine_id">
                    <el-select
                      v-model="dataForm.machine_id"
                      :placeholder="i18n('common.please_select') + i18n('business.quote.normal.machine_id')"
                      filterable clearable>
                      <el-option v-for="(v, k) in machineList" :label="v.chinese_title" :key="k" :value="v.id"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.detail_info') }}</span>
              </div>
            </template>

            <el-card class="mt10" v-for="(option, index) in dataForm.detail" :key="index" shadow="never">
              <div class="text item">
                <el-row :gutter="20">
                  <el-col :span="7">
                    <el-form-item :label="i18n('business.quote.normal.detail.cargo_type')" :prop="'detail.' + index + '.cargo_type'">
                      <el-input v-model="option.cargo_type" :placeholder="i18n('common.please_input') + i18n('business.quote.normal.detail.cargo_type')" clearable></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="7">
                    <el-form-item :label="i18n('business.quote.normal.detail.clause')" :prop="'detail.' + index + '.clause'">
                      <el-input v-model="option.clause" :placeholder="i18n('common.please_input') + i18n('business.quote.normal.detail.clause')" clearable></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="7">
                    <el-form-item :label="i18n('business.quote.normal.detail.price')" :prop="'detail.' + index + '.price'">
                      <el-input v-model="option.price" :placeholder="i18n('common.please_input') + i18n('business.quote.normal.detail.price')" clearable></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="3">
                    <el-button class="ml10 pl22" type="success" link @click.prevent="addOption()">
                      {{ i18n('common.create') }}
                    </el-button>
                    <el-button type="danger" link @click.prevent="removeOption(option)">
                      {{ i18n('common.delete') }}
                    </el-button>
                  </el-col>
                </el-row>
              </div>
            </el-card>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="business-quote-normal-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { isEmpty } from '@/utils/validate'

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/quote/normal')

  const loading = ref(false)
  const machineList = ref([])
  const harborList = ref([])

  const dataForm = reactive({
    id: 0,
    harbor_code: '',
    machine_id: '',
    publish_time: '',
    detail: [{
      cargo_type: '',
      clause: '',
      price: '',
    }],
  })

  const dataRule = reactive({
    publish_time: [{
      required: true,
      message: i18n('business.quote.normal.rules.publish_time.require'),
      trigger: 'blur',
    }],
    harbor_code: [{
      required: true,
      message: i18n('business.quote.normal.rules.harbor_code.require'),
      trigger: 'blur',
    }],
    machine_id: [{
      required: true,
      message: i18n('business.quote.normal.rules.machine_id.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.machine_id = data.data.machine_id
            dataForm.harbor_code = data.data.harbor_code
            dataForm.publish_time = data.data.publish_time
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/detail/select`),
              method: 'get',
              params: $http.adornParams({
                normal_id: dataForm.id,
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                dataForm.detail = []
                data.data.forEach((res) => {
                  const temp = {}
                  temp.cargo_type = res.cargo_type
                  temp.clause = res.clause
                  temp.price = res.price

                  dataForm.detail.push(temp)
                })
              }
            })
          }
        })
      }
    })
  }

  const loadMachineList = () => {
    $http({
      url: $http.adornUrl('/business/information/machine/select'),
      method: 'get',
      params: $http.adornParams({
        category_id: 1,
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        machineList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }


  const loadHarborList = (name) => {
    if (name) {
      loading.value = true
      $http({
        url: $http.adornUrl('/business/harbor/select'),
        method: 'get',
        params: $http.adornParams({
          english_name: name
        }),
      }).then(({ data }) => {
        if (data && data.status === 200) {
          harborList.value = data.data
          loading.value = false
        } else {
          $message.error(data.message)
        }
      })
    } else {
      harborList.value = []
    }
  }

  const addOption = () => {
    // 校验
    if (dataForm.detail.length > 0) {
      let option = dataForm.detail[dataForm.detail.length - 1]
      if (isEmpty(option.cargo_type)) {
        $message.error(i18n('business.information.placeholder_input_option'));
        return
      }

      dataForm.detail.push({ cargo_type: '', clause: '', price: '' })
    } else {
      dataForm.detail.push({ cargo_type: '', clause: '', price: '' })
    }
  }

  const removeOption = (item) => {
    let index = dataForm.detail.indexOf(item)
    if (index !== -1) {
      dataForm.detail.splice(index, 1)
    }
  }

  onMounted(() => {
    init(formRef)

    loadMachineList()
  })
</script>

<style scoped lang="scss">
  .pl22 {
    padding-left: 22px;
  }
</style>
